package f;

import java.io.File;

public class Root {
	
	private Root() {}
	
	private static File root;
	private static File db;
	private static boolean inited;
	
	public static synchronized boolean initTheRoot(String rootPath) {
		File root = new File(rootPath);
		initTheRoot(root, true);
		return inited;
	}
	
	public static synchronized void initTheRoot(File root, boolean initDb) {
		if(inited) {
			return;
		}
		if(root!=null && root.exists() && root.isDirectory()) {
			Root.root = root;
			Root.db = new File(Root.root, "jmjm.db");
			if(initDb) {
				Db.defaultDb().init();				
			}
			inited = true;
		}
	}
	
	public static File theRoot() {
		return Root.root;
	}
	
	public static File theDbFile() {
		return Root.db;
	}
	
	public static File theConfigFile() {
		if(Root.root!=null) {
			return new File(Root.root, "config.properties");
		}
		return null;
	}
	
	public static File theHdFile() {
		if(Root.root!=null) {
			return new File(Root.root, "hardpassword");
		}
		return null;
	}
	
	public static String getFPath(File f) {
		String path = f.getAbsolutePath();
		path = path.replace("\\", "/");
		String rpath = root.getAbsolutePath();
		rpath = rpath.replace("\\", "/");
		if(path.startsWith(rpath)) {
			return path.substring(rpath.length());
		}
		return null;
	}

}
